import {
    getTopBanners,
    getHotArtist,
    getHotRecommends,
    getTopList
} from "../../../../../request/recommend";
import {
    GET_HOT_NEWSONG,
    GET_HOT_ORIGINAL,
    GET_HOT_SOARING,
    GET_RECOMMEND_BANNERS,
    GET_RECOMMEND_HOT,
    GET_RECOMMEND_SONGS_LIST
} from "./actionTypes";

export const getTopBannersAction = () => {
    return (dispatch) => {
        getTopBanners().then((res) => {
            dispatch(topBannersAction(res.banners));
        }).catch((error) => {
            console.log(`error`, error);
        });
    }
}

export const getSongsAction = () => {
    return (dispatch) => {
        getHotArtist().then((res) => {
            // res.list.artists
            const artists = res.list.artists;
            const data = artists.slice(0, 5);
            console.log(`data`, data);
            dispatch(songsAction(data));
        }).then((error) => {
            console.log(`error`, error);
        });
    }
}

export const getHotRecommendAction = () => {
    return (dispatch) => {
        getHotRecommends().then((res) => {
            dispatch(hotRecommnedAction(res.result));
        }).then((error) => {
            console.log(`error`, error);
        });
    }
}


export const getHotLeaderboardAction = (idx) => {
    return (dispatch) => {
        getTopList(idx).then((res) => {
            switch (idx) {
                case 0:
                    dispatch(hotNewSongAction(res.playlist));
                    break;
                case 2:
                    dispatch(hotOriginalAction(res.playlist));
                    break;
                default:
                    dispatch(hotSoaringAction(res.playlist));
            }
        }).then((error) => {
            console.log(`error`, error);
        });
    }
}

// private
const topBannersAction = (banners) => ({
    banners,
    type: GET_RECOMMEND_BANNERS
});

const songsAction = (data) => ({
    artistsList: data,
    type: GET_RECOMMEND_SONGS_LIST
});

const hotRecommnedAction = (data) => ({
    hotRecommnedList: data,
    type: GET_RECOMMEND_HOT
});

const hotNewSongAction = (data) => ({
    hotNewSongList: data,
    type: GET_HOT_NEWSONG
});

const hotSoaringAction = (data) => ({
    hotSoaringList: data,
    type: GET_HOT_SOARING
});

const hotOriginalAction = (data) => ({
    hotOriginalList: data,
    type: GET_HOT_ORIGINAL
});
